package com.zhkj.txg.module.home.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.chad.library.adapter.base.entity.node.BaseNode
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.module.home.entity.CategoryAllResponse
import com.zhkj.txg.module.home.entity.SearchProductResponse
import com.zhkj.txg.module.home.entity.ShareInfoResponse
import com.zhkj.txg.module.home.entity.ShopCategoryResponse
import com.zhkj.txg.module.home.model.HomeManager

@Suppress("UNCHECKED_CAST")
class CategoryViewModel(application: Application) : BaseViewModel(application) {
    private val homeManager = HomeManager()
    val cateLiveData = MutableLiveData<HttpResponse<ShopCategoryResponse>>()
    val categoryAllLiveData = MutableLiveData<HttpResponse<CategoryAllResponse>>()
    val productListLiveData = MutableLiveData<HttpResponse<SearchProductResponse>>()
    val shareInfoLiveData = MutableLiveData<HttpResponse<ShareInfoResponse>>()

    private val numb = 20
    var page = 1
        private set

//    fun searchProduct(
//        token: String, shopId: Int, catId: Int, shopCatId: Int, sort: String,
//        asc: Boolean, type: Int, searchKey: String, restart: Boolean = false
//    ) {
//        val listener = object :
//            ViewModelSubscribeListener<SearchProductResponse>(productListLiveData) {
//            override fun onSuccess(resp: SearchProductResponse) {
//                resp.hasMore = resp.data.size >= numb
//                resp.loadMore = page > 1
//                super.onSuccess(resp)
//                page += 1
//            }
//        }
//        if (restart) page = 1
//        val searchProduct = homeManager.searchProduct(
//            token, shopId, catId, shopCatId, sort,
//            if (asc) "asc" else "desc",
//            type, searchKey, page, numb, listener
//        )
//        dispose.add(searchProduct)
//    }

    fun searchProduct(
        token: String, shopId: Int, activeType: Int?, search: String?, catId: Int?, sort: String?,
        sortAsc: Boolean = false, recommend: Int?, restart: Boolean = false
    ) {
        val listener = object :
            ViewModelSubscribeListener<SearchProductResponse>(productListLiveData) {
            override fun onSuccess(resp: SearchProductResponse) {
                resp.hasMore = resp.data.size >= numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
        val searchProduct = homeManager.searchProduct(
            token, shopId, activeType, search, catId, sort,
            if (sortAsc) "asc" else "desc", page.toString(), numb.toString(), recommend, listener
        )
        dispose.add(searchProduct)
    }

    fun categoryAll() {
        val listener =
            object : ViewModelSubscribeListener<CategoryAllResponse>(categoryAllLiveData) {
                override fun onSuccess(resp: CategoryAllResponse) {
                    for (item in resp.data) for (second in item.child) second.childNode =
                        second.child as? MutableList<BaseNode>
                    super.onSuccess(resp)
                }
            }
        val categoryAll = homeManager.categoryAll(listener)
        dispose.add(categoryAll)
    }

    fun shareInfo(id: Long) {
        val listener = ViewModelSubscribeListener(shareInfoLiveData)
        val shareInfo = homeManager.shareInfo(AccountManager.getToken(), id, listener)
        dispose.add(shareInfo)
    }


    fun shopCate(shopId: Int): MutableLiveData<HttpResponse<ShopCategoryResponse>> {
        val listener = object : ViewModelSubscribeListener<ShopCategoryResponse>(cateLiveData) {
            override fun onSuccess(resp: ShopCategoryResponse) {
                for (cate in resp.data) {
                    cate.childNode = cate.child as MutableList<BaseNode>
                }
                super.onSuccess(resp)
            }
        }
        val shopCate = homeManager.shopCate(shopId, listener)
        dispose.add(shopCate)
        return cateLiveData
    }
}